<%@page contentType="text/html;charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>登录程序之验证页面</title>
</head>
<body>
<%!
    public static String[] userids = {"zhangwengyuan", "douxiaoning", "wangjiahui"};
    public static String[] usernames = {"张文渊", "窦小柠", "王嘉辉"};
    public static String[] userpws = {"123", "456", "789"};

    String check(String userid, String userpw) {
        int i;
        for (i = 0; i < userids.length; i++) {
            if (userids[i].equals(userid)) {
                break;
            }
        }
        if (i < userids.length) {
            if (userpws[i].equals(userpw)) {
                return usernames[i];
            } else {
                return "";
            }
        } else {
            return "";
        }
    }
%>
<%
    request.setCharacterEncoding("UTF-8");
    String id = request.getParameter("id");
    String password = request.getParameter("password");
    String remenber = request.getParameter("remenber");
    String name;
    Cookie[] cookies;
    if ((!(name = check(id, password)).equals(""))) {
        cookies = request.getCookies();
        int i;
        boolean cookies_flag = false; //表示是否创建Cookie
        if (cookies != null) {
            for (i = 0; i < cookies.length; i++) {
                if ("Cookie2019".equals(cookies[i].getName())) {
                    break;
                }
            }
            if (i < cookies.length) {
                cookies_flag = false;
            } else {
                cookies_flag = true;
            }
        } else {
            cookies_flag = true;
        }

        Cookie cookie;

        if (remenber != null) {
            if (cookies_flag) {
                cookie = new Cookie("Cookie2019", id + "#" + password);
                cookie.setMaxAge(45);
                response.addCookie(cookie);
            }
        } else {
            if (!cookies_flag) {
                cookie = new Cookie("Cookie2019", id + "#" + password);
                cookie.setMaxAge(0);
                response.addCookie(cookie);
            }
        }

        session.setAttribute("username", name);
        session.setMaxInactiveInterval(60);
        response.sendRedirect("login_success.jsp");

    } else {              //登录失败，跳转到失败页
        response.sendRedirect("login_failure.jsp");
    }
%>
</body>
